﻿namespace SFWS.XHandler.Core
{
    using SFWS.XHandler.Core.Properties;
    using SFWS.XJSON.Core;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;

    public class AWebPage : Page
    {
        private string actionname = null;
        private string getActionName = null;
        private SFWS.XJSON.Core.JsonSerializer json = new SFWS.XJSON.Core.JsonSerializer();
        private string jsonDateStringFormat = null;
        private string postActionName = null;
        protected object ResponseJsonObject = null;
        protected string XErrorMessage = null;
        protected string XErrorScript = null;
        protected object XJsonObject = null;
        protected string XScript = null;

        protected string F(string name)
        {
            return (this.Context.Request.Form[name] ?? "");
        }

        protected T FromJSON<T>(string json)
        {
            return this.JsonSerializer.Deserialize<T>(json, this.JsonDateStringFormat);
        }

        protected T FromJSON<T>(string json, string dateFormat)
        {
            return this.JsonSerializer.Deserialize<T>(json, dateFormat);
        }

        protected object FromJSON(string json, Type targetType)
        {
            return this.JsonSerializer.Deserialize(json, targetType, this.JsonDateStringFormat);
        }

        protected object FromJSON(string json, Type targetType, string dateFormat)
        {
            return this.JsonSerializer.Deserialize(json, targetType, dateFormat);
        }

        protected string HtmlEncode(string txt)
        {
            return this.HtmlEncode(txt, true);
        }

        protected string HtmlEncode(string txt, bool space2Entity)
        {
            return (space2Entity ? AppUtility.HtmlEncode(txt).Replace(" ", "&nbsp;") : AppUtility.HtmlEncode(txt));
        }

        public bool IsAction(string actionName)
        {
            return ((this.GetActionName == actionName.ToLower()) || (this.PostActionName == actionName.ToLower()));
        }

        protected bool IsGetFor(string actionName)
        {
            return (!string.IsNullOrEmpty(actionName) && ((this.Context.Request.HttpMethod == "GET") && (this.GetActionName == actionName.ToLower())));
        }

        protected bool IsPostFor(string actionName)
        {
            return (!string.IsNullOrEmpty(actionName) && ((this.Context.Request.HttpMethod == "POST") && (this.PostActionName == actionName.ToLower())));
        }

        protected string JsString(string str)
        {
            return this.ToJSON(str).Trim(new char[] { '"' });
        }

        protected string P(string name)
        {
            return (this.Context.Request.QueryString[name] ?? (this.Context.Request.Form[name] ?? ""));
        }

        public virtual void ProcessRequest()
        {
            base.ProcessRequest(this.Context);
        }

        public sealed override void ProcessRequest(HttpContext context)
        {
            if (!(this.IsLogined || AppUtility.IsLoginExclude(base.GetType().BaseType)))
            {
                AppUtility.Log("000");
                AppUtility.NotifyLogin();
            }
            else if (!(this.HasKeys || AppUtility.IsLoginExclude(base.GetType().BaseType)))
            {
                AppUtility.Log("001");
                AppUtility.NotifyAccessInvalid();
            }
            else
            {
                string str = AppUtility.ContextValidate(context, base.GetType().BaseType);
                if (!string.IsNullOrEmpty(str))
                {
                    AppUtility.Log("403");
                    AppUtility.NotifyErrorStatus(0x193, str);
                }
                else
                {
                    AppUtility.Log(context.Response.StatusCode.ToString());
                    JsonObject val = new JsonObject();
                    try
                    {
                        context.Response.AppendHeader("Cache-Control", "no-cache");
                        context.Response.AppendHeader("Pragma", "no-cache");
                        context.Response.AppendHeader("Expires", "0");
                        this.ProcessRequest();
                    }
                    catch (Exception exception)
                    {
                        string str2 = string.Format(Resources.UnHandledException, AppUtility.Log(exception));
                        val.Add("error", new JsonObject().Add("errorType", HandlerErrorType.Exception).Add("message", str2));
                    }
                    if ((val.Count < 1) && (!string.IsNullOrEmpty(this.XErrorScript) || !string.IsNullOrEmpty(this.XErrorMessage)))
                    {
                        val.Add("error", new JsonObject().Add("script", this.XErrorScript).Add("errorType", HandlerErrorType.Other).Add("message", this.XErrorMessage));
                    }
                    if (val.Count < 1)
                    {
                        if (this.XJsonObject != null)
                        {
                            val.Add("data", this.XJsonObject);
                        }
                        if (!string.IsNullOrEmpty(this.XScript))
                        {
                            val.Add("script", this.XScript);
                        }
                    }
                    if (val.Count > 0)
                    {
                        context.Response.AppendHeader(Resources.Json_HeaderName, this.JsonSerializer.Serialize(val));
                    }
                    if (this.ResponseJsonObject != null)
                    {
                        context.Response.Write(this.ToJSON(this.ResponseJsonObject));
                    }
                }
            }
        }

        protected string Q(string name)
        {
            return (this.Context.Request.QueryString[name] ?? "");
        }

        protected string ToJSON(object data)
        {
            return this.JsonSerializer.Serialize(data, this.JsonDateStringFormat);
        }

        protected string ToJSON(object data, string dateFormat)
        {
            return this.JsonSerializer.Serialize(data, dateFormat);
        }

        private string actionName
        {
            get
            {
                if (this.actionname == null)
                {
                    this.actionname = AppUtility.GetConfValue(Resources.ConfSectionName, Resources.ConfKey_ActionName) ?? Resources.ActionName;
                }
                return this.actionname;
            }
        }

        protected string AppPath
        {
            get
            {
                return AppUtility.AppPath;
            }
        }

        private string GetActionName
        {
            get
            {
                if (this.getActionName == null)
                {
                    this.getActionName = (this.Context.Request.QueryString[this.actionName] ?? "").ToLower();
                }
                return this.getActionName;
            }
        }

        private bool HasKeys
        {
            get
            {
                if ((this.Identifications == null) || (this.Identifications.Count<string>() < 1))
                {
                    return true;
                }
                string str = AppUtility.GetConfValue(Resources.ConfSectionName, Resources.ConfKey_IdKeysSessionName) ?? Resources.IdKeysSessionName;
                IEnumerable<string> source = this.Context.Session[str] as IEnumerable<string>;
                if ((source == null) || (source.Count<string>() < 1))
                {
                    return false;
                }
                return (source.Intersect<string>(this.Identifications).Count<string>() > 0);
            }
        }

        public virtual IEnumerable<string> Identifications
        {
            get
            {
                return null;
            }
        }

        protected bool IsLogined
        {
            get
            {
                try
                {
                    return (this.Context.Session[AppUtility.GetConfValue(Resources.ConfSectionName, Resources.ConfKey_IsLoginedSessionName) ?? Resources.IsLoginedSessionName] != null);
                }
                catch
                {
                    return false;
                }
            }
        }

        protected string JsonDateStringFormat
        {
            get
            {
                return this.jsonDateStringFormat;
            }
            set
            {
                this.jsonDateStringFormat = value;
                this.JsonSerializer.DateFormat = value;
            }
        }

        protected SFWS.XJSON.Core.JsonSerializer JsonSerializer
        {
            get
            {
                return this.json;
            }
        }

        private string PostActionName
        {
            get
            {
                if (this.postActionName == null)
                {
                    this.postActionName = (this.Context.Request.Form[this.actionName] ?? "").ToLower();
                }
                return this.postActionName;
            }
        }
    }
}

